<?php
namespace app\api\model;
use think\Model;
use think\Db;

class Single extends Model{
    protected $type = [
        'id'    =>  'integer',
        'type'    =>  'integer',
        'is_review'    =>  'integer',
        'income_review'    =>  'integer',
        'outlay_review'    =>  'integer',
        'cost_price'    =>  'float',
        'pay_price'    =>  'float',
        'sell_price'    =>   'float'
    ];
    //关联收入表
    public function client(){
        return $this->hasOne('client','id','client_id')->field('id,company_name');
    }
    public function guest(){
        return $this->hasOne('guest','id','guest_id')->field('id,guest_name');
    }
    public function supplier(){
        return $this->hasOne('supplier','id','supplier_id')->field('id,supplier_name');
    }
    public function kind(){
        return $this->hasOne('category','id','kind_id')->field('id,title');
    } 
    public function salesman(){
        return $this->hasOne('manager','id','salesman_id')->field('id,cn_name');
    } 
    public function operator(){
        return $this->hasOne('manager','id','operator_id')->field('id,cn_name');
    }
    public function branch(){
        return $this->hasOne('category','id','branch_id')->field('id,title');
    }
    public function category(){
        return $this->hasOne('category','id','category_id')->field('id,title');
    } 
    public function receipts(){
        return $this->hasMany('Receipt','income_id','id')->field('id,income_id,money,type,in,is_review');
    } 
    public function inInvoices(){
        return $this->hasMany('InInvoices','income_id','id')->field('id,company_name,invoice_date,invoice_price,type,inout,income_id');
    }
    //关联支出表
    public function payments(){
        return $this->hasMany('payment','outlay_id','id')->field('id,outlay_id,money,type,in,is_review');;
    } 
    public function Lists($filter = null){
        $where = [
            'type' => $filter['type']
        ];
        $whereOr=[];
        $page = 1;
        $pageCount = 10;
        $type = $filter['type'];
        
        if(!empty($filter)){
            //指定id查询
            if(!empty($filter['id'])){
                $where['id'] = $filter['id'];
            }           
            if(!empty($filter['is_departure']) && $filter['is_departure'] == 1){
                //按出发日期查询
                if(!empty($filter['start_date']) && !empty($filter['end_date'])){
                    $where['start_date'] = [['>= time',$filter['start_date']],['<= time',$filter['end_date']]];
                }else{
                    if(!empty($filter['start_date'])){
                        $where['start_date'] = ['>= time',$filter['start_date']];  
                    }
                    if(!empty($filter['end_date'])){
                        $where['start_date'] = ['<= time',$filter['end_date']];  
                    }
                }                
            }else{
                //按录入日期查询
                if(!empty($filter['start_date']) && !empty($filter['end_date'])){
                    $where['booking_date'] = [['>= time',$filter['start_date']],['<= time',$filter['end_date']]];
                }else{
                    if(!empty($filter['start_date'])){
                        $where['booking_date'] = ['>= time',$filter['start_date']];  
                    }
                    if(!empty($filter['end_date'])){
                        $where['booking_date'] = ['<= time',$filter['end_date']];  
                    }
                }  
            }
            //查询团号
            if(!empty($filter['sn'])){
                $where['sn'] = ['like',"%{$filter['sn']}%"];
            }
            //模糊查询           
            if(!empty($filter['keyword'])){
                $where['product_name|remark'] = ['like',"%{$filter['keyword']}%"];               
            }
            //页码            
            $page = !empty($filter['page']) ? $filter['page'] : $page;
            $pageCount = !empty($filter['page_count']) ? $filter['page_count'] : $pageCount;
            //按客户查询
            if(!empty($filter['client_id'])){
                $where['client_id'] = $filter['client_id'];
            }          
            //按供应商查询
            if(!empty($filter['supplier_id'])){   
                $where['supplier_id'] = $filter['supplier_id'];
            }
            //按所属查询
            if(!empty($filter['category_id'])){
                $where['category_id'] = $filter['category_id'];
            }
            //目的地查询           
            if(!empty($filter['destination'])){
                $where['destination'] = $filter['destination'];         
            }
            //按部门查询
            if(!empty($filter['branch_id'])){
                $where['branch_id'] = $filter['branch_id'];
            }
            //按业务员查询
            if(!empty($filter['salesman_id'])){
                $where['salesman_id'] = $filter['salesman_id'];
            }
            //按操作员查询
            if(!empty($filter['operator_id'])){
                $where['operator_id'] = $filter['operator_id'];
                $whereOr="find_in_set({$filter['operator_id']},be_sales)";
            }
        }
        
        $datas = $this->where($where)->whereOr($whereOr)->with(['payments'=>function($request)use($type){$request->where('type',$type);}]) ->with(['receipts'=>function($request)use($type){$request->where('type',$type);}])
        ->with([
            'kind'=>'kind',
            'supplier'=>'supplier',
            'client'=>'client',           
            'kind'=>'kind',
            'salesman'=>'salesman',
            "category"=>'category',
            "branch"=>'branch',
            'operator'=>'operator'
        ])
        ->page("$page,$pageCount")->order('start_date', 'desc')->select();
        foreach ($datas as &$v) {  
          
            $arr=[];
            foreach(explode(',', $v['guest_id']) as $g){               
                $s=Db::name("guest")->field("id,guest_name")->where("id",$g)->find();
                array_push($arr,$s["guest_name"]);
            }
          
            $v['guest']=implode('，',$arr);
           
            $monthly = Db::name("monthly")->where("year_month",date("Y-m",strtotime($v['start_date'])))->find();  
            if($monthly){
             $v['is_monthly'] =  1;
            }else{
             $v['is_monthly'] =  0;
            } 
            $v['receipt_review']=0;
            $v['payment_review']=0;
            foreach($v['receipts'] as $r){
                if($r['is_review']){
                    $v['receipt_review']=1;
                }
            } 
            foreach($v['payments'] as $r){
                if($r['is_review']){
                    $v['payment_review']=1;
                }
            }              
     }
        $dataCount =  $this->where($where)->whereOr($whereOr)->count();       
        $datalist = array(
            'datas' => $datas,            
            'dataCount' => $dataCount
        );
        return $datalist;      
    }

    public function allLists($filter = null){
        $where = [
            'type' => $filter['type']
        ];
        $page = 1;
        $pageCount = 10;
        $type = $filter['type'];
        
        if(!empty($filter)){
            //指定id查询
            if(!empty($filter['id'])){
                $where['id'] = $filter['id'];
            }           
            if(!empty($filter['is_departure']) && $filter['is_departure'] == 1){
                //按出发日期查询
                if(!empty($filter['start_date']) && !empty($filter['end_date'])){
                    $where['start_date'] = [['>= time',$filter['start_date']],['<= time',$filter['end_date']]];
                }else{
                    if(!empty($filter['start_date'])){
                        $where['start_date'] = ['>= time',$filter['start_date']];  
                    }
                    if(!empty($filter['end_date'])){
                        $where['start_date'] = ['<= time',$filter['end_date']];  
                    }
                }                
            }else{
                //按录入日期查询
                if(!empty($filter['start_date']) && !empty($filter['end_date'])){
                    $where['booking_date'] = [['>= time',$filter['start_date']],['<= time',$filter['end_date']]];
                }else{
                    if(!empty($filter['start_date'])){
                        $where['booking_date'] = ['>= time',$filter['start_date']];  
                    }
                    if(!empty($filter['end_date'])){
                        $where['booking_date'] = ['<= time',$filter['end_date']];  
                    }
                }  
            }
            //查询团号
            if(!empty($filter['sn'])){
                $where['sn'] = ['like',"%{$filter['sn']}%"];
            }
            //模糊查询           
            if(!empty($filter['keyword'])){
                $where['product_name|sn'] = ['like',"%{$filter['keyword']}%"];               
            }
            //页码            
            $page = !empty($filter['page']) ? $filter['page'] : $page;
            $pageCount = !empty($filter['page_count']) ? $filter['page_count'] : $pageCount;
            //按客户查询
            if(!empty($filter['client_id'])){
                $where['client_id'] = $filter['client_id'];
            }
             //按供应商查询
             if(!empty($filter['supplier_id'])){   
                $where['supplier_id'] = $filter['supplier_id'];
            }
            //按所属查询
            if(!empty($filter['category_id'])){
                $where['category_id'] = $filter['category_id'];
            }
            //按部门查询
            if(!empty($filter['branch_id'])){
                $where['branch_id'] = $filter['branch_id'];
            }
            //按业务员查询
            if(!empty($filter['salesman_id'])){
                $where['salesman_id'] = $filter['salesman_id'];
            }
            //按操作员查询
            if(!empty($filter['operator_id'])){
                $where['operator_id'] = $filter['operator_id'];
            }
        }
        
        $datas = $this->where($where)->with(['payments'=>function($request)use($type){$request->where('type',$type);}]) ->with(['receipts'=>function($request)use($type){$request->where('type',$type);}])
        ->with([
            'kind'=>'kind',
            'supplier'=>'supplier',
            'client'=>'client',          
            'kind'=>'kind',
            'salesman'=>'salesman',
            "category"=>'category',
            "branch"=>'branch',
            'operator'=>'operator'
        ])
        ->order('start_date', 'desc')->select();
        foreach ($datas as &$v) {  
            $arr=[];
            foreach(explode(',', $v['guest_id']) as $g){               
                $s=Db::name("guest")->field("id,guest_name")->where("id",$g)->find();
                array_push($arr,$s["guest_name"]);
            }
          
            $v['guest']=implode('，',$arr);

            $monthly = Db::name("monthly")->where("year_month",date("Y-m",strtotime($v['start_date'])))->find();  
            if($monthly){
             $v['is_monthly'] =  1;
            }else{
             $v['is_monthly'] =  0;
            }           
         }
       
        return $datas;      
    }
    public function singleExcel($filter = null){
        $where = [
            'type' => $filter['type']
        ];
        $page = 1;
        $pageCount = 10;
        $type = $filter['type'];
        
        if(!empty($filter)){
            //指定id查询
            if(!empty($filter['id'])){
                $where['id'] = $filter['id'];
            }           
            if(!empty($filter['is_departure']) && $filter['is_departure'] == 1){
                //按出发日期查询
                if(!empty($filter['start_date']) && !empty($filter['end_date'])){
                    $where['start_date'] = [['>= time',$filter['start_date']],['<= time',$filter['end_date']]];
                }else{
                    if(!empty($filter['start_date'])){
                        $where['start_date'] = ['>= time',$filter['start_date']];  
                    }
                    if(!empty($filter['end_date'])){
                        $where['start_date'] = ['<= time',$filter['end_date']];  
                    }
                }                
            }else{
                //按录入日期查询
                if(!empty($filter['start_date']) && !empty($filter['end_date'])){
                    $where['booking_date'] = [['>= time',$filter['start_date']],['<= time',$filter['end_date']]];
                }else{
                    if(!empty($filter['start_date'])){
                        $where['booking_date'] = ['>= time',$filter['start_date']];  
                    }
                    if(!empty($filter['end_date'])){
                        $where['booking_date'] = ['<= time',$filter['end_date']];  
                    }
                }  
            }
            //查询团号
            if(!empty($filter['sn'])){
                $where['sn'] = $filter['sn'];
            }
           //模糊查询           
           if(!empty($filter['keyword'])){
            $where['product_name'] = ['like',"%{$filter['keyword']}%"];               
        }
            //页码            
            $page = !empty($filter['page']) ? $filter['page'] : $page;
            $pageCount = !empty($filter['page_count']) ? $filter['page_count'] : $pageCount;
            //按客户查询
            if(!empty($filter['client_id'])){
                $where['client_id'] = $filter['client_id'];
            }
            //按所属查询
            if(!empty($filter['category_id'])){
                $where['category_id'] = $filter['category_id'];
            }
            //按部门查询
            if(!empty($filter['branch_id'])){
                $where['branch_id'] = $filter['branch_id'];
            }
            //按业务员查询
            if(!empty($filter['salesman_id'])){
                $where['salesman_id'] = $filter['salesman_id'];
            }
            //按操作员查询
            if(!empty($filter['operator_id'])){
                $where['operator_id'] = $filter['operator_id'];
            }
        }
        
        $datas = $this->where($where)->with(['payments'=>function($request)use($type){$request->where('type',$type);}]) ->with(['receipts'=>function($request)use($type){$request->where('type',$type);}])->page("$page,$pageCount")->order('id', 'desc')->select();
        foreach ($datas as &$v) {  
            $receivable=0;
            $payable=0;     
            $received=0;   
            $pay=0;   
           
            $receivable=$v['sell_price']*$v['num'];     
            //已收款               
            $receipt = Db::name("receipt")->where(['type'=>$type,'income_id'=>$v['id']])->select();  
            foreach ($receipt as $v) {
                $received+=$v['money'];
            }          
            //应付         
            $payable = $v['cost_price']*$v['num'];
            //未收款
            $payment = Db::name("payment")->where(['type'=>$type,'outlay_id'=>$v['id']])->select();  
            foreach ($payment as $v) {
                $pay+=$v['money'];
            }
            $v['receivable'] = $receivable;
            $v['payable'] = $payable;
            $v['received'] = $received;
            $v['pay'] = $pay;
        }        
        
        return $datas;      
    }
}